package cn.com.dcsgo.service.impl;

import cn.com.dcsgo.domain.Prod;
import cn.com.dcsgo.dto.PageDTO;
import cn.com.dcsgo.feign.ProdFeignClient;
import cn.com.dcsgo.utils.AuthUtil;
import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.com.dcsgo.domain.MemberCollection;
import cn.com.dcsgo.service.MemberCollectionService;
import cn.com.dcsgo.mapper.MemberCollectionMapper;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 针对表【member_collection(会员收藏表)】的数据库操作Service实现
 *
 * @author Dcsgo
 * @since 2025-10-30 15:57:19
 */
@Slf4j
@Service
public class MemberCollectionServiceImpl extends ServiceImpl<MemberCollectionMapper, MemberCollection>
        implements MemberCollectionService {
    @Resource
    private ProdFeignClient prodFeignClient;

    @Override
    public Long getCollectionCount() {
        return count(
                new LambdaQueryWrapper<MemberCollection>()
                        .eq(MemberCollection::getOpenId, AuthUtil.getOpenid())
        );
    }

    @Override
    public Page<Prod> getCollectProd(PageDTO pageDTO) {
        Page<MemberCollection> collectionPage = new Page<>(pageDTO.getCurrent(), pageDTO.getSize());
        Page<Prod> result = new Page<>(pageDTO.getCurrent(), pageDTO.getSize());
        page(collectionPage);
        log.debug("getCollectProd:collectionPage:{}", JSON.toJSONString(collectionPage));
        result.setPages(collectionPage.getPages());
        log.debug("getCollectProd:result:{}", JSON.toJSONString(result));
        List<MemberCollection> memberCollectionList = collectionPage.getRecords();
        if (memberCollectionList.isEmpty()) {
            return result;
        }
        //RPC 获取商品信息
        Set<Long> prodIds = memberCollectionList.stream().map(MemberCollection::getProdId).collect(Collectors.toSet());
        List<Prod> prodList = prodFeignClient.getBasicProdInfoByIds(prodIds);
        result.setRecords(prodList);
        result.setTotal(prodList.size());
        return result;
    }

    @Override
    public Boolean isCollected(String openid, Long prodId) {
        return exists(
                new LambdaQueryWrapper<MemberCollection>()
                        .eq(MemberCollection::getProdId, prodId)
                        .eq(MemberCollection::getOpenId, openid)
        );
    }

    @Override
    public boolean addOrCancel(String openid, Long prodId) {
        MemberCollection collection = getOne(
                new LambdaQueryWrapper<MemberCollection>()
                        .eq(MemberCollection::getProdId, prodId)
                        .eq(MemberCollection::getOpenId, openid)
        );
        if (collection == null) {
            // 添加收藏
            MemberCollection saveData = new MemberCollection();
            saveData.setOpenId(openid);
            saveData.setProdId(prodId);
            return save(saveData);
        } else {
            // 取消收藏（删除收藏数据）
            return removeById(collection.getId());
        }
    }
}




